﻿@using System.Net.Http.Json;
<MCard Color="red lighten-2"
       Dark>
    <MCardTitle Class="text-h5 red lighten-3">
        Search for Public APIs
    </MCardTitle>
    <MCardText>
        Explore hundreds of free API's ready for consumption! For more information visit
        <a class="grey--text text--lighten-3"
           href="https://github.com/toddmotto/public-apis"
           target="_blank">the GitHub repository</a>.
    </MCardText>
    <MCardText>
        <MAutocomplete TItem="Item"
                       TValue="string"
                       TItemValue="string"
                       @bind-Value="_value"
                       Items="Items"
                       Loading="_isLoading"
                       OnSearchInputUpdate="UpdateSearchInputAsync"
                       OnSelectedItemUpdate="UpdateItemAsync"
                       Color="white"
                       HideNoData
                       HideSelected
                       ItemText="r=>r.Description"
                       ItemValue="r=>r.API"
                       Label="Public APIs"
                       Placeholder="Start typing to Search"
                       PrependIcon="mdi-database-search"></MAutocomplete>
    </MCardText>
    <MDivider></MDivider>
    <ExpandTransition>
        <MList If="@_show"
               class="red lighten-3">
            @for (var i = 0; i < Fields.Count; i++)
            {
                var field = Fields.ElementAt(i);
                <MListItem @key="i">
                    <MListItemContent>
                        <MListItemTitle>
                            @field.Value
                        </MListItemTitle>
                        <MListItemSubtitle>
                            @field.Key
                        </MListItemSubtitle>
                    </MListItemContent>
                </MListItem>
            }
        </MList>
    </ExpandTransition>
    <MCardActions>
        <MSpacer></MSpacer>
        <MButton Disabled="_model==null"
                 Color="grey darken-3"
                 OnClick="Clear">
            Clear
            <MIcon Right>
                mdi-close-circle
            </MIcon>
        </MButton>
    </MCardActions>
</MCard>

@code {
    public class Item
    {
        public string API { get; set; }

        public string Description { get; set; }

        public string Auth { get; set; }

        public string HTPPS { get; set; }

        public string Cors { get; set; }

        public string Link { get; set; }

        public string Category { get; set; }
    }

    public class Result
    {
        public int Count { get; set; }

        public List<Item> Entries { get; set; }
    }

    private int _descriptionLimit = 60;
    private List<Item> _entries = new List<Item>();
    private bool _isLoading;
    private Item _model;
    private string _value;
    private bool _show => !string.IsNullOrEmpty(_value);

    [Inject]
    public HttpClient HttpClient { get; set; }

    protected List<Item> Items
    {
        get
        {
            return _entries.Select(entry =>
            {
                var item = new Item();

                item.API = entry.API;
                item.Auth = entry.Auth;
                item.Category = entry.Category;
                item.Cors = entry.Cors;
                item.Description = entry.Description.Length > _descriptionLimit ? entry.Description.Substring(0, _descriptionLimit) + "..." : entry.Description;
                item.HTPPS = entry.HTPPS;
                item.Link = entry.Link;

                return item;
            }).ToList();
        }
    }

    protected Dictionary<string, object> Fields
    {
        get
        {
            if (_model == null)
            {
                return new Dictionary<string, object>();
            }

            return new Dictionary<string, object>
                {
                    {"API",_model.API},
                    {"Auth",_model.Auth},
                    {"Category",_model.Category},
                    {"Cors",_model.Cors},
                    {"Description",_model.Description},
                    {"HTPPS",_model.HTPPS},
                    {"Link",_model.Link}
                };
        }
    }

    public async Task UpdateSearchInputAsync(string val)
    {
        if (Items.Count > 0)
        {
            return;
        }

        if (_isLoading)
        {
            return;
        }

        _isLoading = true;

        var res = await HttpClient.GetFromJsonAsync<Result>("https://api.publicapis.org/entries");
        _entries = res.Entries.Take(20).ToList();//We can't process too many items now

        _isLoading = false;
    }

    public async Task UpdateItemAsync(Item item)
    {
        _model = item;
    }

    public void Clear()
    {
        _model = null;
        _value = null;
    }
 }
